/*!
 * 通用消息模块
 */

require( './style.less' );
define( [
  'jquery',
  'transit',
  'constants',
  'bigweNotice'
], function( $, transit, constants, BigweNotice ) {
  return {
    showTime: 3e3,
    waringColor: '#eaa000',

    /**
     * ---------------------------------------------------
     * 通用消息展示模块
     * @param content 展示内容
     * @param hideTime消息显示时间 建议使用showTime 不填为一直存在 
     * @param fun 消息消失后的回调
     * @return {[type]}
     */
    show: function( content, hideTime, fun ) {
      if ( !content ) {
        return;
      }
      //clearTimeout( this.timeoutId );
      $( ".show_title_message.js_permanent", window.top.document ).transit( {
        opacity: 0
      }, function() {
        $( this ).remove();
      } );

      var $msg = $( '<div class="show_title_message"><div>' + content + '</div></div>' ).appendTo( window.top.document.body );

      var outerWidth = $msg.outerWidth();
      !hideTime && $msg.addClass( 'js_permanent' );
      $msg.css( 'margin-left', '-' + outerWidth / 2 + 'px' ).transition( {
        top: 0,
        opacity: 1
      }, function() {
        if ( typeof hideTime === "undefined" ) {
          if ( fun ) {
            fun();
          }
        } else {
          setTimeout( function() {
            $msg.transition( {
              top: -40,
              opacity: 0
            }, function() {
              $( this ).remove();
              if ( fun ) {
                fun();
              }
            } );
          }, hideTime < this.showTime ? this.showTime : hideTime );
        }
      } );
      // 20151113 将当前消息提示框返回出去，可随意操作 sherlock
      return $msg.find( '>div' );
    },
    // ---------------------------------------------------
    // 手动隐藏正在显示的消息框
    hide: function( fun ) {
      $( ".show_title_message", window.top.document ).transition( {
        top: -40
      }, function() {
        $( this ).remove();
        if ( $.isFunction( fun ) ) {
          fun();
        }
      } );
    },
    // ---------------------------------------------------
    // 显示一个警告框
    showWarning: function( content, hideTime, fun ) {
      content && this.show( content, hideTime, fun ).css( 'background-color', this.waringColor );
      return false;
    },
    // 显示一个通知消息
    notice: function( msg, dft ) {

      var noticeOption = {
        icon: constants.resPath + '/images/bigwe_face.png',
        title: '微知',
        //tag: 'BigweNotice'
      }
      msg = typeof msg == 'object' ? msg : {
        body: msg
      };

      function bigweNoticeHandle( msg ) {
        return new BigweNotice( msg ).show()
      }

      if ( window.Notification && Notification.permission !== "denied" && !dft ) {
        Notification.requestPermission( function( status ) {
          if ( status == 'granted' ) {
            var options = $.extend( {}, noticeOption, msg );
            var n = new Notification( options.title, options );
            n.onclick = function() {
              window.open( '/notice', 'notice' );
              this.close();
            }
          } else {
            return bigweNoticeHandle( msg );
          }
        } );

      } else {
        return bigweNoticeHandle( msg );
      }
    }
  }
} );
